package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.ServicesModel;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Krzysztof
 */
@Stateful
public class ServicesDAOImpl implements ServicesDAO {

    private EntityManager entityManager;

    public ServicesDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }

    @Override
    public List<ServicesModel> getServicesAll() {
        return (List<ServicesModel>) entityManager.createNamedQuery("ServicesModel.findAll").getResultList();
    }

    @Override
    public ServicesModel getServicesByID(Integer Id) {
        return (ServicesModel) entityManager.createNamedQuery("ServicesModel.findByServiceId").setParameter("serviceId", Id).getSingleResult();
    }

    @Override
    public int getServicesSize() {
        return entityManager.createNamedQuery("ServicesModel.findAll").getResultList().size();
    }

    @Override
    public void addService(ServicesModel service) {
        entityManager.getTransaction().begin();
        entityManager.persist(service);
        entityManager.flush();
        entityManager.refresh(service);
        entityManager.getTransaction().commit();
    }

    @Override
    public void updateService(ServicesModel service) {
        ServicesModel rowToUpdate = getServicesByID(service.getServiceId());

        entityManager.getTransaction().begin();
        rowToUpdate.setClientId(service.getClientId());
        rowToUpdate.setEployeeId(service.getEployeeId());
        rowToUpdate.setModificationDate(service.getModificationDate());
        rowToUpdate.setComment(service.getComment());
        entityManager.getTransaction().commit();
    }

    @Override
    public void removeService(Integer id) {
        ServicesModel rowToDelete = getServicesByID(id);
        entityManager.getTransaction().begin();
        entityManager.remove(rowToDelete);
        entityManager.getTransaction().commit();
    }

    @Override
    public void removeService(ServicesModel srv) {
        entityManager.getTransaction().begin();
        ServicesModel toBeRemoved = entityManager.merge(srv);
        entityManager.remove(toBeRemoved);
        entityManager.getTransaction().commit();
    }

    @Override
    public List<ServicesModel> getServicesByModificationDate(Date modificationDate) {
        return (List<ServicesModel>) entityManager.createNamedQuery("ServicesModel.findByModificationDate").setParameter("modificationDate", modificationDate).getResultList();
    }

    @Override
    public List<ServicesModel> getServicesByClientId(Integer id) {
        return (List<ServicesModel>) entityManager.createNamedQuery("ServicesModel.findByClientId").setParameter("clientId ", id).getResultList();
    }

    @Override
    public int getByModificationDateSize(Date modificationDate) {
        List<ServicesModel> list = (List<ServicesModel>) entityManager.createNamedQuery("ServicesModel.findByModificationDate").setParameter("modificationDate", modificationDate).getResultList();
        return list.size();
    }

    @Override
    public List<ServicesModel> getServicesByClient(ClientsModel cl) {
        List<ServicesModel> list = (List<ServicesModel>) entityManager.createNamedQuery("ServicesModel.findByClient").setParameter("clientId", cl).getResultList();
        return list;
    }
}